package com.cynteka.cyfoman.core;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * User: Sergey
 * Date: 10.08.11
 * Time: 14:31
 */
public class SimpleRoleProvider implements RoleProvider {

    private Set<String> defaultRoles;

    private Map<String, Set<String>> userRoles;

    public Map<String, Set<String>> getUserRoles() {
        return userRoles;
    }

    public void setUserRoles(Map<String, Set<String>> userRoles) {
        this.userRoles = userRoles;
    }


    @Override
    public boolean isAuthorized() {
        return true;
    }

    @Override
    public String getCurrentUser() {
        return System.getProperty("user.name");
    }

    @Override
    public Set<String> getCurrentRoles() {
        Set<String> res = userRoles.get(getCurrentUser());
        if(res == null)
            res = defaultRoles;
        return Collections.unmodifiableSet(res);
    }

    @Override
    public boolean hasRole(String role) {
        return getCurrentRoles().contains(role);
    }

    @Override
    public boolean hasAtLeastOneRole(Set<String> roles) {
        for(String role : roles ) {
            if(hasRole(role))
                return true;
        }
        return false;
    }

    public Set<String> getDefaultRoles() {
        return defaultRoles;
    }

    public void setDefaultRoles(Set<String> defaultRoles) {
        this.defaultRoles = defaultRoles;
    }
}
